# System-wide .zshrc file for zsh(1), sourced only for interactive shells.
#
# Global order: zshenv, zprofile, zshrc, zlogin

# If ZSH startup files exist both in $HOME and XDG-based config directory,
# print a warning (but only if running as a login shell). Note that XDG-based
# config directory is used only when no ZSH startup files are found in $HOME
# (see /etc/zsh/zshenv).
if [[ -z "${ZDOTDIR-}" && -o login ]]; then
	_xdgdir=${XDG_CONFIG_HOME:-$HOME/.config}/zsh
	if _xdgrcs=("$_xdgdir"/.z{shenv,profile,shrc,login}(N)) && (( $#_xdgrcs )); then
		_homercs=("$HOME"/.z{shenv,profile,shrc,login}(N:t))
		print "Warning: Found Zsh startup files both in ~/ and ${_xdgdir/$HOME/\~}/, the latter will\n" \
		       "        be ignored (tip: move $_homercs to ${_xdgdir/$HOME/\~}/)." >&2
	fi
	unset _xdgdir _xdgrcs _homercs
fi

# TIP: If you don't want any plugins to be automatically loaded here, add
# `zsh_plugin_dirs=()` to your $ZDOTDIR/.zshenv.
if (( ! ${+zsh_plugin_dirs} )); then
	zsh_plugin_dirs=("${XDG_DATA_HOME:-$HOME/.local/share}/zsh/plugins")
fi

# TIP: If you want to automatically use all ZSH plugins installed from Alpine
# packages, add `ZSH_LOAD_SYSTEM_PLUGINS=yes` to your $ZDOTDIR/.zshenv.
if [[ "${ZSH_LOAD_SYSTEM_PLUGINS-}" = [1ty]* ]]; then
	zsh_plugin_dirs+=('/usr/share/zsh/plugins')
fi

# Find plugins (per Oh-My-Zsh convention) under $zsh_plugin_dirs (array of
# paths) and add them to $fpath.
_plugins=()
for _dir ($zsh_plugin_dirs); do
	for _name ("$_dir"/*(N:t)); do  # equivalent of 'basename $i'
		if [[ -f "$_dir/$_name/$_name.plugin.zsh" ]]; then
			_plugins+=("$_dir/$_name/$_name.plugin.zsh")
			fpath=("$_dir/$_name" $fpath)
		fi
	done
done

# Source each <name>.zsh script in /etc/zsh/zshrc.d/ which <name> is not
# included in $zshrcd_blacklist.
#
# TIP: If you want to prevent some scripts in /etc/zsh/zshrc.d from being
# loaded, say a.zsh and b.zsh, add `zshrcd_blacklist=(a b)` to your
# $ZDOTDIR/.zshenv ($XDG_CONFIG_HOME/zsh/.zshenv).
zshrcd_blacklist+=()
_scripts=(/etc/zsh/zshrc.d/*.zsh(N:t:r))  # equivalent of 'basename $i .zsh'
for _name (${_scripts:|zshrcd_blacklist}); do  # :| is a set difference A \ B
	source "/etc/zsh/zshrc.d/$_name.zsh"
done

# Source all plugins found in $zsh_plugin_dirs.
for _plugin ($_plugins); do
	source "$_plugin"
done

unset _dir _name _plugin _plugins _scripts
unset zsh_plugin_dirs zshrcd_blacklist ZSH_LOAD_SYSTEM_PLUGINS

# vim: set ft=zsh ts=4:
